package com.yl.ifanr.db;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat; 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.SQLException;
import android.database.Cursor;
import android.util.Log;

import com.yl.ifanr.db.RssItemBean;
/**
 * RSS表模型，封装对RSS表的操作
 * @author hhq163
 *
 */
public class RssDaoimpl extends BaseDao{
	private static final String TAG = "RssDbAdapter";
	public static final String TABLE_NAME = "t_rss_item";
	
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private final Context mCtx;
	
	public RssDaoimpl(Context ctx){
		this.mCtx = ctx;
		mDbHelper = new DatabaseHelper(mCtx);
	}
	/**
	 * 关闭数据库连接
	 */
	public void close(){
		mDbHelper.close();
	}
	/**
	 * 查询最新的一条记录
	 * @return
	 */
	public RssItemBean getItem(){
		RssItemBean rssItemBean = new RssItemBean();
		rssItemBean = getItem(0);
		return rssItemBean;
	}
	/**
	 * 查询一条记录
	 * @param rowId
	 * @return Object
	 * @throws SQLException
	 */
	public RssItemBean getItem(int rowId) throws SQLException {
		RssItemBean rssItemBean = new RssItemBean();
		String sql;
		try {
			Log.d(TAG, "rowId:"+rowId);
			if(rowId==0){
				Log.d(TAG, "获取本地最新信息");
				sql = "select * from " + TABLE_NAME + " order by created desc limit 0,1;";
			}else{
				sql = "select * from " + TABLE_NAME + "id= " + rowId;
			}
			mDb = mDbHelper.getWritableDatabase();
			
			Cursor cursor = mDb.rawQuery(sql,null);
			while (cursor.moveToNext()) {
				rssItemBean.setId(cursor.getInt(cursor.getColumnIndex("id")));
				rssItemBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
				rssItemBean.setLink(cursor.getString(cursor.getColumnIndex("link_url")));
				rssItemBean.setDescription(cursor.getString(cursor.getColumnIndex("desc")));
				rssItemBean.setContent(cursor.getString(cursor.getColumnIndex("content")));
				rssItemBean.setPubdate(cursor.getLong(cursor.getColumnIndex("created")));				
			}
			
			if (cursor != null) {
				cursor.close();
			}
		} catch (Exception e){
			Log.d(TAG, e.getMessage());
		} finally{
			mDb.close();
		}
		
		return rssItemBean;
	}
	/**
	 * 获取所有记录
	 * @return
	 */
	public List<RssItemBean> getAllItem(){
		List<RssItemBean> rssItemList = new ArrayList<RssItemBean>();
		
		try {
			mDb = mDbHelper.getWritableDatabase();
			
			Cursor cursor = mDb.rawQuery("select * from " + TABLE_NAME +" order by id desc;" ,null);
			while (cursor.moveToNext()) {
				RssItemBean rssItemBean = new RssItemBean();
				rssItemBean.setId(cursor.getInt(cursor.getColumnIndex("id")));
				rssItemBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
				rssItemBean.setLink(cursor.getString(cursor.getColumnIndex("link_url")));
				rssItemBean.setDescription(cursor.getString(cursor.getColumnIndex("desc")));
				rssItemBean.setContent(cursor.getString(cursor.getColumnIndex("content")));
				rssItemBean.setPubdate(cursor.getLong(cursor.getColumnIndex("created")));				
				rssItemList.add(rssItemBean);
			}
			
			if (cursor != null) {
				cursor.close();
			}
		} catch (Exception e){
			Log.d(TAG, e.getMessage());
		} finally{
			mDb.close();
		}
		
		return rssItemList;
	}
	/**
	 * 插入一条记录
	 * @param name 轨迹名称
	 * @param desc　描述
	 * @return　long
	 */
	public boolean insert(List<RssItemBean> rssItemList) {
		boolean result = false;
		long pubdate;
		long ltime = 0;
		RssItemBean lastItemBean = this.getItem();
		try {
			if(lastItemBean.getPubdate()!= 0){
				ltime = lastItemBean.getPubdate();
			}
			Log.d(TAG, "希望程序是对的，已获取的本地最新的新闻时间:");
			Log.d(TAG, String.valueOf(ltime));
			mDb = mDbHelper.getWritableDatabase();
			
			for (int i = 0; i < rssItemList.size(); i++) {
				RssItemBean rssItemBean = rssItemList.get(i);
				pubdate = rssItemBean.getPubdate();
				Log.d(TAG, "记录："+String.valueOf(pubdate));
				Log.d(TAG, "记录标题："+rssItemBean.getTitle());
				
				if(pubdate>ltime || pubdate==0){
					mDb.execSQL("insert into " + TABLE_NAME + "(title,link_url,desc,content,created) values(?,?,?,?,?)",
							new Object[]{
								rssItemBean.getTitle(),
								rssItemBean.getLink(),
								rssItemBean.getDescription(),
								rssItemBean.getContent(),
								pubdate
							});
				}
			}
			result = true;
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}finally{
			mDb.close();
		}
		
		return result;
	}
	/**
	 * 删除数据
	 * @param idArray
	 * @return
	 */
	public boolean delete(int[] idArray){
		boolean result = false;
		try {
			mDb = mDbHelper.getWritableDatabase();
			
			if (idArray == null || idArray.length ==0) {
				return result;
			}
			
			String sqlStr = "delete from " + TABLE_NAME + " WHERE id in(";
			for (int i = 0; i < idArray.length; i++) {
				if (i != (idArray.length - 1)) {
					sqlStr += idArray[i] + ",";
				}else {
					sqlStr += idArray[i] + ")";
				}
				
			}
			
			mDb.execSQL(sqlStr);
			
			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}finally{
			mDb.close();
		}
		return result;
	}
	
	
}